Conditions | 1 |
Paths | 4 |
Total Lines | 171 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
36 | function ($scope, CredentialService, $translate) { |
||
37 | $scope.hello = 'world'; |
||
38 | |||
39 | $scope.credentialProperties = [ |
||
40 | { |
||
41 | label: 'Label', |
||
42 | prop: 'label', |
||
43 | matching: ['label', 'title', 'name'] |
||
44 | }, |
||
45 | { |
||
46 | label: 'Username', |
||
47 | prop: 'username', |
||
48 | matching: ['username', 'user', 'login', 'login name'] |
||
49 | }, |
||
50 | { |
||
51 | label: 'Password', |
||
52 | prop: 'password', |
||
53 | matching: ['password', 'pass', 'pw'] |
||
54 | }, |
||
55 | { |
||
56 | label: 'TOTP Secret', |
||
57 | prop: 'otp', |
||
58 | matching: ['totp'] |
||
59 | }, |
||
60 | { |
||
61 | label: 'Custom field', |
||
62 | prop: 'custom_field' |
||
63 | }, |
||
64 | { |
||
65 | label: 'Notes', |
||
66 | prop: 'description', |
||
67 | matching: ['notes', 'description', 'comments'] |
||
68 | }, |
||
69 | { |
||
70 | label: 'Email', |
||
71 | prop: 'email', |
||
72 | matching: ['email', 'mail'] |
||
73 | }, |
||
74 | { |
||
75 | label: 'URL', |
||
76 | prop: 'url', |
||
77 | matching: ['website', 'url', 'fulladdress', 'site', 'web site'] |
||
78 | }, |
||
79 | { |
||
80 | label: 'Tags', |
||
81 | prop: 'tags' |
||
82 | }, |
||
83 | { |
||
84 | label: 'Ignored', |
||
85 | prop: null |
||
86 | } |
||
87 | ]; |
||
88 | var tagMapper = function (t) { |
||
89 | return {text: t}; |
||
90 | }; |
||
91 | var rowToCredential = function (row) { |
||
92 | var _credential = PassmanImporter.newCredential(); |
||
93 | for(var k = 0; k < $scope.import_fields.length; k++){ |
||
94 | var field = $scope.import_fields[k]; |
||
95 | if(field){ |
||
96 | if(field === 'otp'){ |
||
97 | _credential.otp.secret = row[k]; |
||
98 | } else if(field === 'custom_field'){ |
||
99 | var key = ($scope.matched) ? $scope.parsed_csv[0][k] : 'Custom field '+ k; |
||
100 | _credential.custom_fields.push({ |
||
101 | 'label': key, |
||
102 | 'value': row[k], |
||
103 | 'secret': 0 |
||
104 | }); |
||
105 | } else if(field === 'tags'){ |
||
106 | if( row[k]) { |
||
107 | var tags = row[k].split(','); |
||
108 | _credential.tags = tags.map(tagMapper); |
||
109 | } |
||
110 | } else{ |
||
111 | _credential[field] = row[k]; |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | return _credential; |
||
116 | }; |
||
117 | |||
118 | |||
119 | $scope.inspectCredential = function (row) { |
||
120 | $scope.inspected_credential = rowToCredential(row); |
||
121 | }; |
||
122 | |||
123 | $scope.csvLoaded = function (file) { |
||
124 | $scope.import_fields = []; |
||
125 | $scope.inspected_credential = false; |
||
126 | $scope.matched = false; |
||
127 | $scope.skipFirstRow = false; |
||
128 | var file_data = file.data.split(','); |
||
129 | file_data = decodeURIComponent(escape(window.atob(file_data[1]))); |
||
130 | /** global: Papa */ |
||
131 | Papa.parse(file_data, { |
||
132 | complete: function(results) { |
||
133 | if(results.data) { |
||
134 | for(var i = 0; i < results.data[0].length; i++){ |
||
135 | var propName = results.data[0][i]; |
||
136 | $scope.import_fields[i] = null; |
||
137 | for(var p = 0; p < $scope.credentialProperties.length; p++){ |
||
138 | var credentialProperty = $scope.credentialProperties[p]; |
||
139 | if(credentialProperty.matching){ |
||
140 | if(credentialProperty.matching.indexOf(propName.toLowerCase()) !== -1){ |
||
141 | $scope.import_fields[i] = credentialProperty.prop; |
||
142 | $scope.matched = true; |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | if($scope.matched){ |
||
148 | $scope.inspectCredential(results.data[1]); |
||
149 | } |
||
150 | $scope.parsed_csv = results.data; |
||
151 | $scope.$apply(); |
||
152 | } |
||
153 | } |
||
154 | }); |
||
155 | }; |
||
156 | |||
157 | var addCredential = function (index) { |
||
158 | function handleState (index) { |
||
159 | if ($scope.parsed_csv[index + 1]) { |
||
160 | $scope.import_progress = { |
||
161 | progress: index / $scope.parsed_csv.length * 100, |
||
162 | loaded: index, |
||
163 | total: $scope.parsed_csv.length |
||
164 | }; |
||
165 | |||
166 | addCredential(index + 1); |
||
167 | } else { |
||
168 | $scope.import_progress = { |
||
169 | progress: 100, |
||
170 | loaded: $scope.parsed_csv.length, |
||
171 | total: $scope.parsed_csv.length |
||
172 | }; |
||
173 | $scope.log.push($translate.instant('done')); |
||
174 | $scope.importing = false; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | var _credential = rowToCredential($scope.parsed_csv[index]); |
||
179 | _credential.vault_id = $scope.active_vault.vault_id; |
||
180 | if (!_credential.label) { |
||
181 | $scope.log.push($translate.instant('import.skipping', {line: index})); |
||
182 | handleState(index); |
||
183 | return; |
||
184 | } |
||
185 | $scope.log.push($translate.instant('import.adding', {credential: _credential.label})); |
||
186 | CredentialService.createCredential(_credential).then(function (result) { |
||
187 | if (result.credential_id) { |
||
188 | $scope.log.push($translate.instant('import.added', {credential: _credential.label})); |
||
189 | handleState(index); |
||
190 | } |
||
191 | }); |
||
192 | }; |
||
193 | |||
194 | $scope.importing = false; |
||
195 | $scope.startCSVImport = function () { |
||
196 | $scope.importing = true; |
||
197 | $scope.log = []; |
||
198 | var start = ($scope.skipFirstRow) ? 1 : 0; |
||
199 | addCredential(start); |
||
200 | }; |
||
201 | |||
202 | $scope.updateExample = function () { |
||
203 | var start = ($scope.skipFirstRow) ? 1 : 0; |
||
204 | $scope.inspectCredential($scope.parsed_csv[start]); |
||
205 | }; |
||
206 | }]); |
||
207 | }()); |